package com.yc.mybatis.mapper;

import com.yc.mybatis.bean.Comments;
import com.yc.mybatis.bean.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface UserMapper {

    @Select("select * from user")
    // resultMap="rm1"
    @ResultMap("rm1")
    List<User> selectAll();

    @Select("select * from user where id = #{id}")
    // Results ==> <resultMap>
    @Results(
            id = "rm1",
            value = {
                    // id = true ==> <id>
                    @Result(column = "id", property = "id", id = true),
                    // Result ==> <result>
                    @Result(column = "head_img", property = "headImg"),
                    @Result(column = "id", property = "comments",
                        // many ==> collection
                            // one 一对一
                            // many 一对多
                        many = @Many(
                                select = "selectCommentsByUid",
                                fetchType = FetchType.LAZY
                        )),
            }
    )
    User selectById( int id);

    @Insert("insert into user (id,username,password,email)" +
            " values (default,#{username},#{password},#{email})")
    @Options(keyColumn = "id",keyProperty = "id",useGeneratedKeys = true)
    int insert(User user);

    @Update("update user set username=#{username},password=#{password}" +
            " where id=#{id}")
    int updateById(User user);

    //@Delete
    @Select("select * from comments where user_id=#{uid}")
    List<Comments> selectCommentsByUid(int uid);

}
